package com.example.dps;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class ActionBarHelper {
	
	Activity activity;
	View actionBarView;
	DBHelper dbhelper;
	
	RelativeLayout headerParentLayout;
	
	
	
	ActionBarHelper(Activity activity){
		this.activity=activity;
//		actionBar = currentActivity.getActionBar();
		dbhelper = new DBHelper(activity);
		
		
		
	}
	
	public void makeBar(){
//        ActionBar actionBar = currentActivity.getActionBar();
//        actionBar.setDisplayShowTitleEnabled(false);
//        actionBar.setDisplayUseLogoEnabled(false);
//        actionBar.setDisplayHomeAsUpEnabled(false);
//        actionBar.setDisplayShowHomeEnabled(false);
//        actionBar.setDisplayShowCustomEnabled(true);
//        
//        
//        this.actionBarView = currentActivity.getLayoutInflater().inflate(R.layout.header_layout, null);
//        actionBar.setCustomView(this.actionBarView);
		RelativeLayout headerLayout = (RelativeLayout) this.activity.findViewById(R.id.headerLayout);
		
		headerLayout.removeAllViews();
		headerLayout.inflate(this.activity, R.layout.header_layout, headerLayout);
		
		this.updateImage();
		
		ImageView homeIcon = (ImageView) headerLayout.findViewById(R.id.headerHomeIcon);
		
		homeIcon.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				Intent intent = new Intent(ActionBarHelper.this.activity, SplashActivity.class);
				ActionBarHelper.this.activity.startActivity(intent);
				
			}
			
		});
		
		
		homeIcon.setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				/** get the root element of the button view **/
		    	ImageView view = (ImageView) v;
		    	
		        if(event.getAction() == MotionEvent.ACTION_DOWN) {
		        	/** change the image button picture when clicked **/
		        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.header_home_icon_clicked));
		        } else if (event.getAction() == MotionEvent.ACTION_UP) {
		        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.header_home_icon));
		        }
		        else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
		        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.header_home_icon));
		        }
				return false;
			}
		});
		
		
		
		
		
		
        
        
        
	}
	
	
	
	public void setCurrentView(String view){
		RelativeLayout headerLayout = (RelativeLayout) this.activity.findViewById(R.id.headerLayout);
		TextView viewText = (TextView) headerLayout.findViewById(R.id.headerActivityText);
		viewText.setText(view);
	}
	
	public void updateImage(){
		

		Drawable image = this.dbhelper.getImage();
		
		if(image != null){
			
			Log.e("actionbarhelper", "image != null");
			
			RelativeLayout headerLayout = (RelativeLayout) this.activity.findViewById(R.id.headerLayout);
			ImageView headerPicture = (ImageView) headerLayout.findViewById(R.id.headerPicture);
			ImageView headerBox = (ImageView) headerLayout.findViewById(R.id.headerPictureBox);
			headerPicture.setImageDrawable(null);
			headerPicture.setImageDrawable(this.resize(image, headerBox));
			
			
			
		}
		
	}
	
	
	
	@SuppressWarnings("deprecation")
	private Drawable resize(Drawable image, View containingView) {
		
		LayoutParams params = (LayoutParams) containingView.getLayoutParams();
		
	    Bitmap d = ((BitmapDrawable)image).getBitmap();
	    Bitmap bitmapOrig = Bitmap.createScaledBitmap(d, 140, 140, false);
	    return new BitmapDrawable(bitmapOrig);
	}

}